/**
 * 通讯录本地数据库操作文件
 */
import CommonUtil from "@/static/utils/common-util"
import DBManage from "@/static/utils/db/client/sqlite.util"
const { v5: uuidv5, v4: uuidv4} = require('uuid');

let contactsDB = {
  tableName: 'contacts',
  tableCols: ["loginId","mobile","userName","version","sex","addressCode","addressName","introduce","spell","headIcon"],
  insert: function(obj, focus, success, fail) {
    let result = CommonUtil.analysisForInsterSql(obj, this.tableCols, focus);
    DBManage.insertOrReplaceData(this.tableName, result.data, result.condition, success, fail);
  },
  updateById: function(obj, focus, success, fail) {
    let result = CommonUtil.analysisForUpdateSql(obj, this.tableCols, focus);
    DBManage.updateTableData(this.tableName, result, "loginId = '" + obj.loginId + "'", success, fail)
  },
  deleteById: function(id, success, fail) {
    DBManage.deleteTableData(this.tableName, "loginId = '" + id + "'", success, fail)
  },
  queryForContacts: function(success, fail) {
    DBManage.selectByOps(this.tableName, "spell, userName", success, fail)
  },
  createTable: function() {
    var sql = "CREATE TABLE IF NOT EXISTS " + this.tableName +" (" +
      "loginId varchar(16) NOT NULL PRIMARY KEY," +
      "mobile char(11) NOT NULL," +
      "userName varchar(50) NOT NULL," +
      "version int(11) NOT NULL," +
      "sex char(1)," +
      "addressCode char(12)," +
      "addressName varchar(100)," +
      "introduce varchar(100)," +
      "spell char(3)," +
      "headIcon varchar(100)" +
      ")";
    DBManage.executeBySql(sql);
  },
  deleteTable: function() {
    DBManage.dropTable(this.tableName);
  }
}

export default contactsDB;